/**
 * 格式化日期
 * @param {string|number|Date} date 日期
 * @param {string} [format='YYYY-MM-DD HH:mm:ss'] 格式
 * @returns {string} 格式化后的日期字符串
 */
export function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
  if (!date) return ''
  
  const d = new Date(date)
  if (isNaN(d.getTime())) return ''
  
  const year = d.getFullYear()
  const month = d.getMonth() + 1
  const day = d.getDate()
  const hours = d.getHours()
  const minutes = d.getMinutes()
  const seconds = d.getSeconds()
  
  const padZero = (num) => String(num).padStart(2, '0')
  
  return format
    .replace('YYYY', year)
    .replace('MM', padZero(month))
    .replace('DD', padZero(day))
    .replace('HH', padZero(hours))
    .replace('mm', padZero(minutes))
    .replace('ss', padZero(seconds))
}

/**
 * 获取相对时间
 * @param {string|number|Date} date 日期
 * @returns {string} 相对时间字符串
 */
export function getRelativeTime(date) {
  if (!date) return ''
  
  const d = new Date(date)
  if (isNaN(d.getTime())) return ''
  
  const now = new Date()
  const diff = now.getTime() - d.getTime()
  
  const minute = 60 * 1000
  const hour = 60 * minute
  const day = 24 * hour
  const month = 30 * day
  const year = 12 * month
  
  if (diff < minute) {
    return '刚刚'
  } else if (diff < hour) {
    return Math.floor(diff / minute) + '分钟前'
  } else if (diff < day) {
    return Math.floor(diff / hour) + '小时前'
  } else if (diff < month) {
    return Math.floor(diff / day) + '天前'
  } else if (diff < year) {
    return Math.floor(diff / month) + '个月前'
  } else {
    return Math.floor(diff / year) + '年前'
  }
}

/**
 * 判断是否同一天
 * @param {string|number|Date} date1 日期1
 * @param {string|number|Date} date2 日期2
 * @returns {boolean} 是否同一天
 */
export function isSameDay(date1, date2) {
  const d1 = new Date(date1)
  const d2 = new Date(date2)
  
  return (
    d1.getFullYear() === d2.getFullYear() &&
    d1.getMonth() === d2.getMonth() &&
    d1.getDate() === d2.getDate()
  )
}

/**
 * 获取日期范围
 * @param {string|number|Date} date 日期
 * @param {number} days 天数
 * @returns {[Date, Date]} 日期范围 [开始日期, 结束日期]
 */
export function getDateRange(date, days) {
  const d = new Date(date)
  const start = new Date(d)
  const end = new Date(d)
  
  start.setDate(d.getDate() - Math.floor(Math.abs(days) / 2))
  end.setDate(d.getDate() + Math.floor(Math.abs(days) / 2))
  
  return [start, end]
}

/**
 * 计算剩余时间
 * @param {string|number|Date} endDate 结束日期
 * @returns {Object} 剩余时间对象 { days, hours, minutes, seconds }
 */
export function getRemainingTime(endDate) {
  const end = new Date(endDate)
  const now = new Date()
  
  const diff = end.getTime() - now.getTime()
  if (diff <= 0) {
    return {
      days: 0,
      hours: 0,
      minutes: 0,
      seconds: 0
    }
  }
  
  const days = Math.floor(diff / (24 * 60 * 60 * 1000))
  const hours = Math.floor((diff % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000))
  const minutes = Math.floor((diff % (60 * 60 * 1000)) / (60 * 1000))
  const seconds = Math.floor((diff % (60 * 1000)) / 1000)
  
  return {
    days,
    hours,
    minutes,
    seconds
  }
} 